package com.rain.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.rain.activity.utils.Globals;
import com.rain.activity.utils.QuestionDAO;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class IndexActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Globals.init(this);
        setContentView(R.layout.activity_index);
        new Thread(){
            @Override
            public void run() {
                    try {
                        SharedPreferences sp = getSharedPreferences("que_flag",MODE_PRIVATE);
                        boolean saveFlag = sp.getBoolean("que_flag",false);
                        if(saveFlag){
                                Thread.sleep(2000);
                        }
                        else {
                            QuestionDAO.delete();
                            InputStream in = getAssets().open("question.txt");
                            IndexActivity.load(in);
                            SharedPreferences.Editor edit = sp.edit();
                            edit.putBoolean("que_flag",true);
                            edit.commit();
                        }
                    }catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                    }
                Intent intent = new Intent(IndexActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
                }
        }.start();

    }
    public static void  load(InputStream in)throws IOException{
        List<Map<String,Object>> values = new ArrayList<>();
        BufferedReader buf = new BufferedReader(new InputStreamReader(in,"UTF-8"));
        String line = null;
        StringBuilder que = new StringBuilder();
        StringBuilder ans = new StringBuilder();
        boolean queFlag = false;
        while((line=buf.readLine())!=null){
            if(line.equals("Start_Question")){
                queFlag = true;
            }else if (line.equals("Start_Answer")){
                queFlag = false;
            }
            else if(line.equals("End_flag")){
                Map<String,Object> map = new HashMap<>();
                map.put("que", que.toString());
                map.put("ans", ans.toString());
                QuestionDAO.insertData(map);
                que = new StringBuilder();
                ans = new StringBuilder();
            }
            else{
                if(queFlag){
                    que.append(line);
                }
                else{
                    ans.append(line);
                }
            }
        }
    }
}
